/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Desktop_PayOK.java
 *
 * Created on Apr 4, 2011, 10:49:22 AM
 */
package DesktopGUIpackage;

import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Address;
import DTO.Item;
import DTO.Order;
import Session.SessionManager;
import Utilities.MoneyFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author edaype
 */
public class Desktop_PayOK extends javax.swing.JPanel {

    private DesktopController desktopController;
    private DesktopControllerHelper desktopControllerHelper;
    private MoneyFormatter moneyFormatter;
    private Order order;
    private static final String[] columnNames = new String[]{"Name", "Quantity", "Price"};
    private SessionManager sessionManager;

    /** Creates new form Desktop_PayOK */
    public Desktop_PayOK(DesktopControllerHelper desktopControllerHelper) {
        initComponents();
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
        moneyFormatter = new MoneyFormatter();
        sessionManager = desktopController.getSessionMgr();
        order = sessionManager.getOrder();
        customInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ButtonLayout = new javax.swing.JPanel();
        AddButton = new javax.swing.JButton();
        TitleLabel = new javax.swing.JLabel();
        ItemPane = new javax.swing.JScrollPane();
        ItemTable = new javax.swing.JTable();
        TotalPanel = new javax.swing.JPanel();
        SubtotalLabel = new javax.swing.JLabel();
        TaxLabel = new javax.swing.JLabel();
        StoreCardLabel = new javax.swing.JLabel();
        TotalLabel = new javax.swing.JLabel();
        SubtotalField = new javax.swing.JTextField();
        DiscountField = new javax.swing.JTextField();
        ShippingCostField = new javax.swing.JTextField();
        TotalOut = new javax.swing.JTextField();
        TaxField = new javax.swing.JTextField();
        ShipLabel = new javax.swing.JLabel();
        ShippingPanel = new javax.swing.JPanel();
        ShippedToLabel = new javax.swing.JLabel();
        NameOut = new javax.swing.JLabel();
        StNoOut = new javax.swing.JLabel();
        AptOut = new javax.swing.JLabel();
        StreetOut = new javax.swing.JLabel();
        CityOut = new javax.swing.JLabel();
        ProvinceOut = new javax.swing.JLabel();
        PostalOut = new javax.swing.JLabel();
        ItemLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        ButtonLayout.setBackground(new java.awt.Color(255, 255, 255));

        AddButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        AddButton.setText("Back to Home");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 48));
        TitleLabel.setText("Order Confirmation");

        javax.swing.GroupLayout ButtonLayoutLayout = new javax.swing.GroupLayout(ButtonLayout);
        ButtonLayout.setLayout(ButtonLayoutLayout);
        ButtonLayoutLayout.setHorizontalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 567, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(AddButton)
                .addGap(18, 18, 18))
        );
        ButtonLayoutLayout.setVerticalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(TitleLabel)
                    .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        ItemTable.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ItemTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "Make", "Model", "Price", "Quantity"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ItemPane.setViewportView(ItemTable);

        TotalPanel.setBackground(new java.awt.Color(255, 255, 255));

        SubtotalLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        SubtotalLabel.setText("Sub-total:");

        TaxLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        TaxLabel.setText("Shipping Cost:");

        StoreCardLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        StoreCardLabel.setText("Discount:");

        TotalLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        TotalLabel.setText("Total:");

        SubtotalField.setFont(new java.awt.Font("Tahoma", 0, 14));

        DiscountField.setFont(new java.awt.Font("Tahoma", 0, 14));

        ShippingCostField.setFont(new java.awt.Font("Tahoma", 0, 14));

        TotalOut.setFont(new java.awt.Font("Tahoma", 0, 14));

        TaxField.setFont(new java.awt.Font("Tahoma", 0, 14));

        ShipLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        ShipLabel.setText("Tax:");

        javax.swing.GroupLayout TotalPanelLayout = new javax.swing.GroupLayout(TotalPanel);
        TotalPanel.setLayout(TotalPanelLayout);
        TotalPanelLayout.setHorizontalGroup(
            TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TotalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(TotalPanelLayout.createSequentialGroup()
                        .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(ShipLabel)
                            .addComponent(TaxLabel))
                        .addGap(18, 18, 18)
                        .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TaxField, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                            .addComponent(ShippingCostField, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)))
                    .addGroup(TotalPanelLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(TotalLabel)
                            .addComponent(SubtotalLabel)
                            .addComponent(StoreCardLabel))
                        .addGap(18, 18, 18)
                        .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(SubtotalField, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                            .addComponent(DiscountField, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                            .addComponent(TotalOut, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))))
                .addContainerGap())
        );
        TotalPanelLayout.setVerticalGroup(
            TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TotalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SubtotalLabel)
                    .addComponent(SubtotalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DiscountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StoreCardLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TaxLabel)
                    .addComponent(ShippingCostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ShipLabel)
                    .addComponent(TaxField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TotalLabel)
                    .addComponent(TotalOut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        ShippingPanel.setBackground(new java.awt.Color(255, 255, 255));

        ShippedToLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        ShippedToLabel.setText("Your order will be shipped to:");

        NameOut.setFont(new java.awt.Font("Tahoma", 0, 14));
        NameOut.setText("Name");

        StNoOut.setFont(new java.awt.Font("Tahoma", 0, 14));
        StNoOut.setText("St No.");

        AptOut.setFont(new java.awt.Font("Tahoma", 0, 14));
        AptOut.setText("Apt #");

        StreetOut.setFont(new java.awt.Font("Tahoma", 0, 14));
        StreetOut.setText("Street Name");

        CityOut.setFont(new java.awt.Font("Tahoma", 0, 14));
        CityOut.setText("City Name");

        ProvinceOut.setFont(new java.awt.Font("Tahoma", 0, 14));
        ProvinceOut.setText("Province Name");

        PostalOut.setFont(new java.awt.Font("Tahoma", 0, 14));
        PostalOut.setText("Postal");

        javax.swing.GroupLayout ShippingPanelLayout = new javax.swing.GroupLayout(ShippingPanel);
        ShippingPanel.setLayout(ShippingPanelLayout);
        ShippingPanelLayout.setHorizontalGroup(
            ShippingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ShippingPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ShippingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ShippingPanelLayout.createSequentialGroup()
                        .addGroup(ShippingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ShippedToLabel)
                            .addGroup(ShippingPanelLayout.createSequentialGroup()
                                .addComponent(StNoOut, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(AptOut, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(StreetOut, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(ShippingPanelLayout.createSequentialGroup()
                        .addComponent(PostalOut, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ProvinceOut, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70))
                    .addGroup(ShippingPanelLayout.createSequentialGroup()
                        .addComponent(CityOut, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(ShippingPanelLayout.createSequentialGroup()
                        .addComponent(NameOut, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        ShippingPanelLayout.setVerticalGroup(
            ShippingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ShippingPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ShippedToLabel)
                .addGap(22, 22, 22)
                .addComponent(NameOut)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(ShippingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StNoOut)
                    .addComponent(AptOut)
                    .addComponent(StreetOut, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CityOut)
                .addGap(12, 12, 12)
                .addGroup(ShippingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PostalOut)
                    .addComponent(ProvinceOut))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        ItemLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        ItemLabel.setText("Items in your Order:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(313, 313, 313)
                        .addComponent(ItemLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ButtonLayout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ItemPane, javax.swing.GroupLayout.PREFERRED_SIZE, 747, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(ShippingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                                .addComponent(TotalPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ItemLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ItemPane, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(TotalPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ShippingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ButtonLayout, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        // TODO add your handling code here:
        desktopController.clearSession();
        desktopControllerHelper.changeFrame(2);
    }//GEN-LAST:event_AddButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JLabel AptOut;
    private javax.swing.JPanel ButtonLayout;
    private javax.swing.JLabel CityOut;
    private javax.swing.JTextField DiscountField;
    private javax.swing.JLabel ItemLabel;
    private javax.swing.JScrollPane ItemPane;
    private javax.swing.JTable ItemTable;
    private javax.swing.JLabel NameOut;
    private javax.swing.JLabel PostalOut;
    private javax.swing.JLabel ProvinceOut;
    private javax.swing.JLabel ShipLabel;
    private javax.swing.JLabel ShippedToLabel;
    private javax.swing.JTextField ShippingCostField;
    private javax.swing.JPanel ShippingPanel;
    private javax.swing.JLabel StNoOut;
    private javax.swing.JLabel StoreCardLabel;
    private javax.swing.JLabel StreetOut;
    private javax.swing.JTextField SubtotalField;
    private javax.swing.JLabel SubtotalLabel;
    private javax.swing.JTextField TaxField;
    private javax.swing.JLabel TaxLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JLabel TotalLabel;
    private javax.swing.JTextField TotalOut;
    private javax.swing.JPanel TotalPanel;
    // End of variables declaration//GEN-END:variables

    private void customInit() {
        initTable();
        setTotals();
        setAddress();
    }

    private void initTable() {
        ListIterator<Item> iter = order.getItemList().listIterator();
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        while (iter.hasNext()) {
            Item item = iter.next();
            Vector row = new Vector();
            row.add(item.getName());
            row.add(item.getQty());
            row.add(moneyFormatter.format(item.getSoldPrice() * item.getQty()));
            model.addRow(row);
            if (item.getDiscountAmount() > 0.0 && sessionManager.getLoggedIn()) {
                row = new Vector();
                row.add("Discount");
                row.add(item.getQty());
                row.add("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()));
                model.addRow(row);
            }
        }

        ItemTable.setModel(model);

        TableColumn column = null;
        column = ItemTable.getColumnModel().getColumn(0);
        column.setPreferredWidth(375); //third column is bigger
        column = ItemTable.getColumnModel().getColumn(1);
        column.setPreferredWidth(25);
        column = ItemTable.getColumnModel().getColumn(2);
        column.setPreferredWidth(50);

        ItemTable.setRowHeight(40);

        ItemTable.setColumnSelectionAllowed(false);
        ItemTable.setRowSelectionAllowed(true);
    }

    private void setTotals() {
        SubtotalField.setText(moneyFormatter.format(order.getItemSubTotal()));
        DiscountField.setText(moneyFormatter.format(order.getTotalItemDiscount()));
        ShippingCostField.setText(moneyFormatter.format(order.getShippingFee()));
        TaxField.setText(moneyFormatter.format(order.getTaxAmount()));
        TotalOut.setText(moneyFormatter.format(order.getGrandTotal()));
    }

    private void setAddress(){
        Address addr = sessionManager.getShipaddressForOrder();
        NameOut.setText(sessionManager.getCustomer().getFirstName() + " " + sessionManager.getCustomer().getLastName());
        AptOut.setText(addr.getAptNo());
        StNoOut.setText(Integer.toString(addr.getStNo()));
        StreetOut.setText(addr.getStreet());
        CityOut.setText(addr.getCity());
        PostalOut.setText(addr.getPostCode());
        ProvinceOut.setText(addr.getProvince());
    }
}
